
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yybb_flutter/Macros/yyb_macros.dart';
import 'package:yybb_flutter/http/api_response.dart';
import 'package:yybb_flutter/modules/incremental_board/models/incremtal_board_culemodel.dart';
import 'package:yybb_flutter/modules/incremental_board/models/yybbincrementailCuleMode.dart';
import 'package:yybb_flutter/modules/incremental_board/vmodels/incrementai_broad_vmodel.dart';
import 'package:yybb_flutter/yybbtools/yybb_loading_Widget.dart';
import 'package:yybb_flutter/yybbtools/yybb_loading_nodata_widget.dart';
import 'package:yybb_flutter/yybbtools/yybb_services.dart';
import 'package:yybb_flutter/yybbtools/yybb_tools.dart';

class IncrementaiSearchCustomerPage extends StatefulWidget {
  @override
  _IncrementaiSearchCustomerPageState createState() => _IncrementaiSearchCustomerPageState();
}

class _IncrementaiSearchCustomerPageState extends State<IncrementaiSearchCustomerPage> {

  List _dataList = [];
  var _page = 1;
  RefreshController _refreshController =  RefreshController(initialRefresh: false);

  @override
  void initState() {
    super.initState();
    _getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: YYBBColor245,
      body: SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        header: YYBBRefreshHeadWidget(),
        footer: YYBBRefreshLoadingWidget(),
        controller: _refreshController,
        onRefresh: _onRefresh,
        onLoading: _onLoading,
        child: _childWitdget(),
      ),
    );
  }

  //  loading 加载
  Widget _childWitdget() {
    Widget _childWidget;
    if (_dataList != null && _dataList.length != 0) {
      _childWidget = ListView.builder(
        itemBuilder: _cellForRow,
        itemCount: _dataList.length > 0 ? _dataList.length : 0,
      );
    } else {
      _childWidget = new YYBBNODataWidget();
      // YYBBPlatform.invokeMapMethod("NodataCallBack");

    }
    return _childWidget;
  }

  // Cell 布局
  Widget _cellForRow(BuildContext context, int index ) {
    YYBBIncrementailCuleModeList _model = _dataList[index];
    // YYBBPrintLog(_model.shortName);
    return FlatButton(
      splashColor: Colors.transparent, // 点击时的高亮效果设置为透明
      onPressed: (){
        _pushOrderDetailPage(_model);
      },
      child: Container(
        margin: EdgeInsets.fromLTRB(0, 12, 0, 0),
        constraints: BoxConstraints(
          minHeight: 10,
        ),
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(4),
            ///圆角
            border: Border.all(color: Colors.white, width: 1)
          ///边框颜色、宽
        ),
        // color: Colors.white,
        child: Container(
          padding:EdgeInsets.fromLTRB(8, 12, 8, 12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                _model.shortName != null?_model.shortName : _model.epName,
                textAlign: TextAlign.left ,
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w700,
                  color: YYBBColor51,
                ),
              ),
              SizedBox(height: 7,),
              Row(
                mainAxisAlignment:MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    "所在区域   ${_model.area != null? _model.area:""}",
                    textAlign: TextAlign.left,
                    style: TextStyle(
                      fontSize: 12,
                      color: YYBBColor119,
                    ),
                  ),
                  SizedBox(height: 8,),
                  Text(
                    "客户距离   ${_model.distance}km",
                    textAlign: TextAlign.left,
                    style: TextStyle(
                      fontSize: 12,
                      color: YYBBColor119,
                    ),
                  ),
                ],
              ),
              SizedBox(height: 7,),
              Container(
                height: 0.5,
                color: YYBBLineColor,
              ),
              SizedBox(height: 10,),
              Row(
                  children: [
                    Text(
                      "预测采购规模   ",
                      textAlign: TextAlign.left ,
                      style: TextStyle(
                        fontSize: 13,
                        // fontWeight: FontWeight.w200,
                        color: YYBBColor119,
                      ),
                    ),
                    Text(
                      "${_model.predictionPurchaseQuantity.toString()}万㎡",
                      textAlign: TextAlign.left ,
                      style: TextStyle(
                        fontSize: 13,
                        fontWeight: FontWeight.w600,
                        color: YYBBColor51,
                      ),
                    ),
                  ]
              ),
              SizedBox(height: 5,),
            ],
          ),
        ),
      ),
    );
  }



  // 获取数据
  void _getData() async {
    var params = Map<String, String>();
    params["currentPage"] = _page.toString();
    params["pageSize"] = "20";
    ApiResponse<YYBBIncrementailCuleMode> item  = await IncrementaiBroadVmodel.getSearchCustomerData(params);
    if(_page == 1) {
      _dataList.clear();
    }
    _dataList.addAll(item.data.list);
    _stopRefresh();
    // // 刷新界面
    setState(() {});
  }


  // 点击进入下一个界面
  void _pushOrderDetailPage(YYBBIncrementailCuleModeList model){
    var params = Map<String, String>();
    params["customerId"] = model.customerId.toString();
    params["enterpriseId"] = model.enterpriseId.toString();
    params["sellerEnterpriseId"] = model.sellerEnterpriseId.toString();
    YYBBPlatform.invokeMethod("jumpCustomerPortraitVC", params);

  }

  //下拉加载
  void _onRefresh() async{
    // await Future.delayed(Duration(milliseconds: 1000));
    _page = 1;
    _getData();
    setState(() {

    });

  }

  //上拉加载
  void _onLoading() async{
    // monitor network fetch
    // await Future.delayed(Duration(milliseconds: 1000));
    _page++;
    _getData();
    if(mounted)
      setState(() {
      });
  }


  // 停止刷新
  void _stopRefresh(){

    if(_dataList.length  < 10 * _page){
      _refreshController.loadNoData();
    }else{
      _refreshController.loadComplete();
    }
    _refreshController.refreshCompleted();
  }

}
